package ua.khpi.si.kit49.diploma.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Storage for the forecast of weather
 * 
 * @author netstalk3r
 * 
 */
public class Forecast implements Comparable<Forecast> {

	private Integer low;
	private Integer high;
	private String day;
	private Date date;
	private String description;
	private SimpleDateFormat dateFormatter;

	/**
	 * Default constructor
	 */
	public Forecast() {
		super();
		dateFormatter = new SimpleDateFormat(Constants.DATE_FORMAT);
	}

	/**
	 * Constructor with parameters
	 * 
	 * @param low
	 *            lower value of temperature
	 * @param high
	 *            upper value of temperature
	 * @param day
	 *            day of the forecast
	 * @param date
	 *            date of the forecast
	 * @param description
	 *            description of the forecast
	 */
	public Forecast(Integer low, Integer high, String day, Date date,
			String description) {
		super();
		this.low = low;
		this.high = high;
		this.day = day;
		this.date = date;
		this.description = description;
		dateFormatter = new SimpleDateFormat(Constants.DATE_FORMAT);
	}

	public Integer getLow() {
		return low;
	}

	public void setLow(Integer low) {
		this.low = low;
	}

	public Integer getHigh() {
		return high;
	}

	public void setHigh(Integer high) {
		this.high = high;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(String date) {
		if (this.date == null) {
			try {
				this.date = dateFormatter.parse(date);
			} catch (ParseException e) {
				this.date = new Date();
			}
		}
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public int hashCode() {
		int hash = 19;
		hash = hash * 17 + ((low == null) ? 0 : low.hashCode());
		hash = hash * 17 + ((high == null) ? 0 : high.hashCode());
		hash = hash * 17 + ((date == null) ? 0 : date.hashCode());
		hash = hash * 17 + ((description == null) ? 0 : description.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Forecast))
			return false;
		Forecast entry = (Forecast) obj;
		return low.equals(entry.getLow()) && high.equals(entry.getHigh())
				&& date.equals(entry.getDate())
				&& description.equals(entry.getDescription());
	}

	@Override
	public int compareTo(Forecast compareForecast) {
		int result = compareForecast.getLow() - this.low;
		if (result != 0)
			return (int) (result / Math.abs(result));
		result = compareForecast.getHigh() - this.high;
		return (int) (result / Math.abs(result));
	}

}
